package com.hxkcloud.patient.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.adapter.DolistPreviewAdapter;
import com.hxkcloud.patient.adapter.NoticeDoAdapter;
import com.hxkcloud.patient.beans.DolistBean;
import com.hxkcloud.patient.popup.CalendarPopWindow;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by william_sun on 2016-07-12.
 * Description:用户执行单预览
 */
public class DolistPreviewActivity extends BaseActivity implements View.OnClickListener, NoticeDoAdapter.onDolistFunctionListener {

    //title
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_modify;

    private TextView tv_name;
    private RecyclerView rv;
    private TextView tv_notice;
    private Button bt_nowuse;
    private Button bt_intimeuse;

    private DolistBean doBean;

    private DolistPreviewAdapter adapter;

    private CalendarPopWindow calendarView;

    private String title;
    private String orderId;
    private String patientId;
    private int type = 0;//0执行单 1提醒单

    private boolean isBack = false;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_DOORDERVIEW_SUCCESS:
                doBean = (DolistBean) msg.obj;
                if (doBean != null) {
                    tv_name.setText(doBean.getOrderName());
                    adapter = new DolistPreviewAdapter(mContext, doBean.getOrderDetailList(), type,
                            this);
                    rv.setAdapter(adapter);
                } else {
                    toastMsg(R.string.datagetfail);
                }
                break;
            case MessageCode.NETWORKCODE_DOORDERVIEW_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                } else {
                    toastMsg(R.string.datagetfail);
                }
                break;
            case MessageCode.NETWORKCODE_DOORDERUSE_SUCCESS:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                } else {
                    toastMsg(R.string.datacommitsuccess);
                }
                Intent iBack = new Intent();
                iBack.putExtra("use", true);
                iBack.putExtra("back", isBack);
                setResult(RESULT_OK, iBack);
                finish();
                break;
            case MessageCode.NETWORKCODE_DOORDERUSE_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                } else {
                    toastMsg(R.string.datacommitfail);
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dolistpreview);

//        doBean = (DolistBean) this.getIntent().getSerializableExtra("item");

        title = this.getIntent().getStringExtra("title");
        orderId = this.getIntent().getStringExtra("id");
        type = this.getIntent().getIntExtra("type", 0);
        patientId = this.getIntent().getStringExtra("patientId");
        isBack = this.getIntent().getBooleanExtra("back", false);

        initView();
        initData();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_modify = (TextView) findViewById(R.id.tv_title_func);

        tv_name = (TextView) findViewById(R.id.tv_dolistpreview_title);
        rv = (RecyclerView) findViewById(R.id.rv_dolistpreview);
        tv_notice = (TextView) findViewById(R.id.tv_dolistpreview_notice);
        bt_nowuse = (Button) findViewById(R.id.bt_dolistpreview_nowuse);
        bt_intimeuse = (Button) findViewById(R.id.bt_dolistpreview_intimeuser);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        if (type == 0) {
            tv_title.setText(getResources().getText(R.string.userdolist));
            tv_notice.setText(R.string.performlist_text_tip);
        } else {
            tv_title.setText(getResources().getText(R.string.remindlist));
            tv_notice.setText(R.string.remindlist_text_tip);
        }
        tv_modify.setVisibility(View.VISIBLE);
        tv_modify.setText(getResources().getText(R.string.modify));
        iv_back.setOnClickListener(this);
        tv_modify.setOnClickListener(this);

        tv_name.setText(title);
        bt_nowuse.setOnClickListener(this);
        bt_intimeuse.setOnClickListener(this);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(mLayoutManager);
//        adapter = new DolistPreviewAdapter(mContext, doBean.getOrderDetailList(), this
//        );
//        rv.setAdapter(adapter);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        loading();
        YiJiaHuNetWorkUtils.doOrderView(orderId, handler);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.tv_title_func:
                Intent iModefy = new Intent(mContext, NewDolistActivity.class);
                iModefy.putExtra("type", type);
                iModefy.putExtra("item", (Serializable) doBean);
                iModefy.putExtra("isMy", doBean.getType().equals("1") ? true : false);
                iModefy.putExtra("patientId", patientId);
                startActivity(iModefy);
                finish();
                break;
            case R.id.bt_dolistpreview_nowuse:
                if (!TextUtils.isEmpty(patientId)) {
                    List<String> patients = new ArrayList<>();
                    patients.add(patientId);
                    Calendar cal = Calendar.getInstance();
                    String date = "" + cal.getTime().getTime();
                    loading();
                    YiJiaHuNetWorkUtils.doOrderUse(patients, date, orderId, handler);
                } else {
                    Intent iPatient = new Intent(mContext, ChooseUserActivity.class);
                    startActivityForResult(iPatient, 123);
                }
                break;
            case R.id.bt_dolistpreview_intimeuser:
                if (calendarView == null) {
                    calendarView = new CalendarPopWindow(this);
                }
                calendarView.setCalendarListener(new CalendarPopWindow.CalendarListener() {
                    @Override
                    public void setCalendar(String calendar) {
                        List<String> patients = new ArrayList<>();
                        patients.add(patientId);
                        chooseDate = 0;
                        long cur = 0;
                        try {
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            chooseDate = sdf.parse(calendar).getTime();
                            Calendar cal = Calendar.getInstance();
                            cur = cal.getTime().getTime();
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        if (chooseDate < cur) {
                            toastMsg(R.string.pleasechoosecorrecttime);
                        } else {
                            if (!TextUtils.isEmpty(patientId)) {
                                loading();
                                YiJiaHuNetWorkUtils.doOrderUse(patients, "" + chooseDate, orderId, handler);
                            } else {
                                Intent iPatient = new Intent(mContext, ChooseUserActivity.class);
                                startActivityForResult(iPatient, 124);
                            }
                        }
                    }
                });
                calendarView.showBottom(ll_title);
                break;
        }
    }

    private long chooseDate = 0;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 123) {
                List<String> patients = data.getStringArrayListExtra("patients");
                Calendar cal = Calendar.getInstance();
                String date = "" + cal.getTime().getTime();
                loading();
                YiJiaHuNetWorkUtils.doOrderUse(patients, date, orderId, handler);
            } else if (requestCode == 124) {
                ArrayList<String> patients = data.getStringArrayListExtra("patients");
                patients.addAll(patients);
                loading();
                YiJiaHuNetWorkUtils.doOrderUse(patients, chooseDate + "", orderId, handler);
            }
        }
    }

    //查看列表文章
    @Override
    public void article(String id) {

    }

    @Override
    public void delete(int pIndex) {

    }

    @Override
    public void modify(int pIndex) {

    }
}
